﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace printer
{
    public class reciptionist : Employee
    {


        public bool register_studio(Studio_customer cstmr)
        {
            int kind_Studio = 3;
            string Query = "insert into Person (Name,Phone,Addres,Kind_Id) values('" + cstmr.Name + "','" + cstmr.phone + "','" + cstmr.Addres + "','" + kind_Studio + "')";
            DataBase.insert_update_delete_in_database(Query);
            return true;
        }

        public double register_Owner(Studio_Owner SOwner)
        {
            int kind_Owner = 1;
            string Query = "insert into Person (Name,Phone,Addres,Kind_Id) values('" + SOwner.Name + "','" + SOwner.phone + "','" + SOwner.Addres + "','" + kind_Owner + "')";
            DataBase.insert_update_delete_in_database(Query);

            SqlDataReader reader = DataBase.select_from_database("SELECT TOP 1 Id FROM Person ORDER BY Id DESC");
            Double Id_owner = 0.0;
            while (reader.Read())
            {
                Id_owner = Convert.ToDouble(reader["Id"]);

            }
            return Id_owner;
            //DataBase.close_connection();
        }
        public bool attatch_Owner_Studio(Studio_Owner SOwner)
        {
            double Id_owner = register_Owner(SOwner);

            SqlDataReader id2 = DataBase.select_from_database("SELECT TOP 1 Id FROM Person where id=" + (Id_owner - 1) + "");
            id2.Read();
            int Id_studio = int.Parse(id2[0].ToString());
            string Query = "insert into Studio_Owner_Ids(Studio_name,Studio_Owner) values('" + Id_studio + "','" + Id_owner + "')";
            DataBase.insert_update_delete_in_database(Query);
            return true;
        }
        public string get_MinUrl1()//core ayman]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
        {
            FolderBrowserDialog Main = new FolderBrowserDialog();
            DialogResult result = Main.ShowDialog();
            Main.ShowNewFolderButton = true;
            if (result.Equals(DialogResult.OK))
            {
                string Main_URL;
                Main_URL = Main.SelectedPath;
                //textBox2.Text = Main_URL;
                return Main_URL;
            }
            return "bad path";
        }

        public List<string>[] orer_list(string url)
        {
            List<string>[] orders = new List<string>[2];
            List<string> order = new List<string>();
            List<string> size_SubfolderName = new List<string>();
            List<string> subfolder_content = new List<string>();
            int subfolder_num = 0;
            DirectoryInfo directory = new DirectoryInfo(url);
            DirectoryInfo[] directories = directory.GetDirectories();
            for (int i = 0; i < directories.Length; i++)
            // foreach (DirectoryInfo folder in directories)
            {
                order.Add(url + "\\" + directories[i].Name);
                size_SubfolderName.Add(directories[i].Name);
                subfolder_num++;

            }


            for (int i = 0; i < order.Count; i++)
            {
                //size_SubfolderName.Add(folder.Name);
                int subfolder_photos = System.IO.Directory.GetFiles(order[i], "*.jpg", SearchOption.AllDirectories).Length;
                subfolder_content.Add(subfolder_photos.ToString());
                //listBox1.Items.Add(a.ToString());
            }
            orders[0] = size_SubfolderName;//name
            orders[1] = subfolder_content;//العدد
            return orders;
        }

        public double make_order(List<string>[]List_order,ref order cust_order, int customer_id) //
        {
            //call cost function 
            //cutmer come with her data (array of order with its data count,type,cost)
            //update the data base of the customer wih the data 
            //return the complete cost of the order
            

         
            List<string> size_SubfolderName = new List<string>();
            List<string> subfolder_content = new List<string>();
            int Employee_Id = 3;
            string t1 = DateTime.Now.ToString("dd-MM-yy HH:mm:ss");
            cust_order.date = DateTime.Now.ToString("dd-MM-yy HH:mm:ss");
           size_SubfolderName= List_order[1];
            subfolder_content=List_order[0];
            //int i = 1;
           // cust_order.order_component[i].type=subfolder_content[i];
            cust_order.order_component[0].count=int.Parse(size_SubfolderName[0]);
            calculate_cost(ref cust_order);
            string Query = "insert into Order (Employee_Id,Studio_Id,Cost,Date) values('" + Employee_Id + "','" + customer_id + "','" + cust_order.order_cost + "','" + t1 + "')";
            DataBase.insert_update_delete_in_database(Query);
            SqlDataReader reader = DataBase.select_from_database("SELECT TOP 1 Id FROM Order ORDER BY Id DESC");
            Double Id_Order = 0.0;
            while (reader.Read())
            {
                Id_Order = Convert.ToDouble(reader["Id"]);

            }
            foreach (component item in cust_order.order_component)
            {
                string Query_Comm = "insert into Component (Order_Id,Type_Id,Count,Total_Cost) values('" + Id_Order + "','" + item.type + "','" + item.count + "','" + item.cost + "')";
                DataBase.insert_update_delete_in_database(Query_Comm);

            }
            double cost = 0.0;
            return cost;
        }
        public double check_balnce(int customer_id)
        {
            //return the balance of the customer
            return 0.0;
        }
        public void pay_balance(double balance)
        {
            //updat the balance in the data base and return with الباقى
        }

        public void calculate_cost(ref order cust_order)     //update
        {
            double cmp_cost = 0;


            foreach (component cmp in cust_order.order_component)
            {
                SqlDataReader reader = DataBase.select_from_database("select Cost from Component_Type where " + "Type" + "='" + cmp.type + "'");
                if (reader.Read())
                {
                    cmp_cost = reader.GetDouble(0) * cmp.count;
                    cust_order.order_cost += cmp_cost;
                }

            }
        }

        public double Delete_Customer(string studio_name)   //tell ayman change parameter  from id to name 
        {
            double studio_id = 0.0;
            SqlDataReader reader = DataBase.select_from_database("select Id from Person where " + "Name" + "='" + studio_name + "'");

            while (reader.Read())
            {
                studio_id = Convert.ToDouble(reader["Id"]);

            }
            if (studio_id == 0)
            {
                return 0;
            }
            else
            {
                double studio_owner = 0.0;
                SqlDataReader reader1 = DataBase.select_from_database("select Studio_Owner from Studio_Owner_Ids where " + "Studio_name" + "='" + studio_id + "'");

                while (reader1.Read())
                {
                    studio_owner = Convert.ToDouble(reader1["Studio_Owner"]);

                }
                string Query_Comm = "delete Studio_Owner_Ids where  " + "Studio_name" + "='" + studio_id + "'";
                DataBase.insert_update_delete_in_database(Query_Comm);
                string Query_Comm2 = "delete Person where  " + "Id" + "='" + studio_id + "'";
                DataBase.insert_update_delete_in_database(Query_Comm2);
                string Query_Comm1 = "delete Person where  " + "Id" + "='" + studio_owner + "'";
                DataBase.insert_update_delete_in_database(Query_Comm1);

            }
            return studio_id;
        }

       
            
    }
   
   
}
